<?
//inculde即可以使用data数据。本页所有的data数据，倾在controller都处理好
include_once("../../../common/module/checkA.php");
include_once("../../../../config.php");
?>




 
<?php


//插入项目信息
$_REQUEST['ai_authur']      =$_SESSION['username'];
$_REQUEST['ai_utoken']      = $_SESSION['utoken'];


//传递过来的页面信息。返回的时候，会根据这些信息返回原来的页面
$nowpageid      = addslashes($_REQUEST['nowpageid']);
$sortby         = addslashes($_REQUEST['sortby']);
$term_nid       = addslashes($_REQUEST['term_nid']);


//设置需要搜索的表格的前缀后缀
$table_suffix = "case";
$table = $table_Prefix . $table_suffix;
//设置需要搜索的表格的前缀后缀


$sql=insert_date($table,$_REQUEST);
$query = mysql_query($sql) or die ("error:".mysql_error());
mysql_close($con);







//==================================
//插入项目之后，为项目插入基本的财务信息。
//首先获取这个最新插入的项目的id，留着备用。


$this_case=select_one_line_all_items($table,"name = '".$_REQUEST['ai_name']."' order by nid desc ");

$_auto_insert['ai_utoken']    = $_SESSION['utoken'];
$_auto_insert['ai_ip']        = $_SERVER["REMOTE_ADDR"];
$_auto_insert['ai_authur']    = $_SESSION['nid'];




//少于1500元的项目，直接收齐，否则收取订金和尾款
if($_REQUEST['ai_sum']<"1501")
{
    $_finance_name="项目收款";
    $_auto_insert['ai_cost']      =$_REQUEST['ai_sum'];
}
else
{
    $_finance_name="项目订金";
    $_auto_insert['ai_cost']      =$_REQUEST['ai_sum']*0.3;
}

$_auto_insert['ai_name']   = $_finance_name;
$_auto_insert['ai_unit']   = $_REQUEST['ai_unit'];
$_auto_insert['ai_caseid'] = $this_case['nid'];
$_auto_insert['ai_status'] = "待收款";




//设置需要搜索的表格的前缀后缀
$table_suffix = "finance";
$table = $table_Prefix . $table_suffix;
//设置需要搜索的表格的前缀后缀


$sql=insert_date($table,$_auto_insert);
$query = mysql_query($sql) or die ("error:".mysql_error());






//插入完项目收款之后，再插入项目支出
$_auto_insert['ai_cost']   ="0";
$_auto_insert['ai_name']   = "项目支出";
$sql=insert_date($table,$_auto_insert);
$query = mysql_query($sql) or die ("error:".mysql_error());


//查看合同金额和实际项目金额，是否对应，不对应的，则插入佣金支出
    
    if($_REQUEST['ai_contract_sum'] > $_REQUEST['ai_sum'])
        {
            $_auto_insert['ai_cost']   = $_REQUEST['ai_contract_sum'] - $_REQUEST['ai_sum'];
            $_auto_insert['ai_name']   = "项目佣金";
            $sql=insert_date($table,$_auto_insert);
            $query = mysql_query($sql) or die ("error:".mysql_error());
    
        }
        







//插入完项目支出，再看看是否需要再录入项目收款
if($_REQUEST['ai_sum']>"1501")
{
    $_auto_insert['ai_cost']   =$_REQUEST['ai_sum']*0.7;
    $_auto_insert['ai_name']   = "项目尾款";
    $sql=insert_date($table,$_auto_insert);
    $query = mysql_query($sql) or die ("error:".mysql_error());
}

mysql_close($con);












//==================================
//上面处理完项目的事情，现在开始处理提成的事情。
//但是新插入的数据，是没有$new_detail_nid传递过来的，所以要拿最新的数据出来。


$this_detail=select_one_line_all_items($table_Prefix."case","nid > 0 order by nid desc");

if($this_detail['unit']!=$_REQUEST['ai_unit'])
{
    exit;
}

$new_detail_nid=$this_detail['nid'];



//第一步：首先不管如何，先删除晒所有属于这个提成的和这个项目的方案。（谁，在哪个项目）

//第二步.然后重新插入这个新的入去。
//记录的准则是：谁，在什么项目，担任什么职位，得到多少评分，根据什么方案，得到多少


//第一步
$delete_bonus_sql=" DELETE FROM ".$table_Prefix."bonus_record WHERE caseid = $new_detail_nid";
$query = mysql_query($delete_bonus_sql) or die ("error:".mysql_error());

//第二步.
$_bonus['ai_authur']      = $_SESSION['username'];
$_bonus['ai_bonus_plane'] = $_REQUEST['ai_bonus'];
$_bonus['ai_caseid']        = $new_detail_nid;


$_bonus['ai_agent']      = $_REQUEST['ai_agent'];
$_bonus['ai_sales']      = $_REQUEST['ai_sales'];
$_bonus['ai_dist_sales'] = $_REQUEST['ai_dist_sales'];
$_bonus['ai_respon']     = $_REQUEST['ai_respon'];
$_bonus['ai_service']    = $_REQUEST['ai_service'];
$_bonus['ai_designer']   = $_REQUEST['ai_designer'];
$_bonus['ai_programer']  = $_REQUEST['ai_programer'];
$_bonus['ai_editor']     = $_REQUEST['ai_editor'];

$_bonus['ai_utoken']    = $_SESSION['utoken'];

//取出计算提成的方案。用于计算最终提成。
$_bonus_plane=select_one_line_all_items($table_Prefix."bonus_plane","nid = '".$_bonus['ai_bonus_plane']."'");


if($_bonus_plane['agent_rank_type']=="percent")
{$_bonus['ai_agent_bonus']     = $_bonus_plane['agent']*$_REQUEST['ai_sum'];}
else
{$_bonus['ai_agent_bonus']     = $_bonus_plane['agent'];}


if($_bonus_plane['sales_rank_type']=="percent")
{$_bonus['ai_sales_bonus']     = $_bonus_plane['sales']*$_REQUEST['ai_sum'];}
else
{$_bonus['ai_sales_bonus']     = $_bonus_plane['sales'];}

if($_bonus_plane['dist_sales_rank_type']=="percent")
{$_bonus['ai_dist_sales_bonus']     = $_bonus_plane['dist_sales']*$_REQUEST['ai_sum'];}
else
{$_bonus['ai_dist_sales_bonus']     = $_bonus_plane['dist_sales'];}


if($_bonus_plane['respon_rank_type']=="percent")
{$_bonus['ai_respon_bonus']     = $_bonus_plane['respon']*$_REQUEST['ai_sum'];}
else
{$_bonus['ai_respon_bonus']     = $_bonus_plane['respon'];}


if($_bonus_plane['service_rank_type']=="percent")
{$_bonus['ai_service_bonus']     = $_bonus_plane['service']*$_REQUEST['ai_sum'];}
else
{$_bonus['ai_service_bonus']     = $_bonus_plane['service'];}


if($_bonus_plane['designer_rank_type']=="percent")
{$_bonus['ai_designer_bonus']     = $_bonus_plane['designer']*$_REQUEST['ai_sum'];}
else
{$_bonus['ai_designer_bonus']     = $_bonus_plane['designer'];}


if($_bonus_plane['programer_rank_type']=="percent")
{$_bonus['ai_programer_bonus']     = $_bonus_plane['programer']*$_REQUEST['ai_sum'];}
else
{$_bonus['ai_programer_bonus']     = $_bonus_plane['programer'];}


if($_bonus_plane['editor_rank_type']=="percent")
{$_bonus['ai_editor_bonus']     = $_bonus_plane['editor']*$_REQUEST['ai_sum'];}
else
{$_bonus['ai_editor_bonus']     = $_bonus_plane['editor'];}




//计算完毕，插入库
$bonus_sql=insert_date($table_Prefix."bonus_record",$_bonus);
$query = mysql_query($bonus_sql) or die ("error:".mysql_error());





?>

<script type="text/javascript">
    alert("输入信息已保存，点击确认返回继续操作.");
    document.location.href = "detail-list.php?nav-itemed=case";
</script>


